package com.first.first.controller;

import com.first.first.pojo.ResponseMessage;
import com.first.first.pojo.User;
import com.first.first.pojo.dto.UserDto;
import com.first.first.pojo.dto.UserEditDto;
import com.first.first.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    IUserService userService;

    @PostMapping()
    public ResponseMessage<User> add(@Validated @RequestBody UserDto user) {
        User result = userService.addUser(user);
        return ResponseMessage.success(result);
    }

    @GetMapping("/{id}")
    public ResponseMessage<User> get(@PathVariable("id") Integer id) {
        User result = userService.getUser(id);
        return ResponseMessage.success(result);
    }

    @PutMapping()
    public ResponseMessage<Object[]> edit(@Validated @RequestBody UserEditDto user) {
        userService.updateUser(user);
        return ResponseMessage.success(new Object[]{});
    }

    @DeleteMapping("/{id}")
    public ResponseMessage<Object[]> delete(@PathVariable("id") Integer id) {
        userService.deleteUser(id);
        return ResponseMessage.success(new Object[]{});
    }
}
